#include "OLED/oled.h"
#include "stdlib.h"
//#include "oledfont.h"  	 
#include "./DELAY/delay.h"
#include "mycode.h"
extern unsigned char F6x8[][6];
extern const unsigned char F8X16[];
extern const char Hzk[][32];
extern const char bitmap4oled[];


//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63		 
//size:选择字体 16/12 
//阴码+逐列式+顺向+C51格式
void OLED_ShowChar_Lora(uint8_t x,uint8_t y,uint8_t chr)
{      	
	unsigned char c=0,i=0;	
	c=chr-' ';//得到偏移后的值			
	if(x>Max_Column-1){x=0;y=y+2;}
	if(SIZE ==16)
	{
		OLED_Set_Pos(x,y);	
		for(i=0;i<8;i++)
			OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
		OLED_Set_Pos(x,y+1);
		for(i=0;i<8;i++)
			OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
	}
	else 
	{	
		OLED_Set_Pos(x,y+1);
		for(i=0;i<6;i++)
			OLED_WR_Byte(F6x8[c][i],OLED_DATA);
	}
}
//显示一个字符号串
//x:0~127
//y:0~63		
void OLED_ShowString_Lora(uint8_t x,uint8_t y,uint8_t *chr)
{
	unsigned char j=0;
	while (chr[j]!='\0')
	{		
		OLED_ShowChar_Lora(x,y,chr[j]);
		x+=8;
		if(x>120){x=0;y+=2;}
			j++;
	}
}
//设置坐标
void OLED_Set_Pos(unsigned char x, unsigned char y) 
{ 
	OLED_WR_Byte(0xb0+y,OLED_CMD);
	OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
	OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD); 
}   	  
//显示汉字
//示例：下面就是no为2和no为3分别存储了温度两个字
//OLED_ShowCHinese(0,2,2);OLED_ShowCHinese(0+16,2,3);//温度
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no)
{      			    
	uint8_t t,adder=0;
	OLED_Set_Pos(x,y);	
	for(t=0;t<16;t++)
	{
		OLED_WR_Byte(buff_all[2*no][t],OLED_DATA);
		adder+=1;
	}	
	OLED_Set_Pos(x,y+1);	
	for(t=0;t<16;t++)
	{	
		OLED_WR_Byte(buff_all[2*no+1][t],OLED_DATA);
		adder+=1;
	}					
}
//显示汉字
void OLED_ShowCHineseArray(uint8_t x,uint8_t y,char arr[32])
{      			    
	uint8_t t,adder=0;
	OLED_Set_Pos(x,y);	
	for(t=0;t<16;t++)
	{
		OLED_WR_Byte(arr[t],OLED_DATA);
		adder+=1;
	}	
	OLED_Set_Pos(x,y+1);	
	for(t=16;t<32;t++)
	{	
		OLED_WR_Byte(arr[t],OLED_DATA);
		adder+=1;
	}					
}

void OLED_DrawBMP2(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
{ 	
	unsigned int j=0;
	unsigned char x,y;

	if(y1%8==0) y=y1/8;      
	else y=y1/8+1;
	for(y=y0;y<y1;y++)
	{
		OLED_Set_Pos(x0,y);
		for(x=x0;x<x1;x++)
		{      
			OLED_WR_Byte(BMP[j++],OLED_DATA);	    	
		}
	}
} 

/***********功能描述：显示显示BMP图片128×64起始点坐标(x,y),x的范围0～127，y为页的范围0～7*****************/
void OLED_DrawBMP(unsigned char x, unsigned char y,unsigned char width, unsigned char hight,unsigned char BMP[])
{
	OLED_DrawBMP2(x, y, x + width, y + hight / 8, BMP);
}

/*  参考初始化
	delay_init(72);	   	 	//延时初始化 
	OLED_Init_Lora();//OLED初始化
	OLED_Clear();
	char buffer[100]={0};
	snprintf(buffer, sizeof(buffer),"MID=%#x dddddddddddddDID=%#x",15,16);
	OLED_ShowString_Lora(0,0,(u8*)buffer);//汉字显示 
	OLED_ShowCHinese(0,2,2);OLED_ShowCHinese(0+16,2,3);//温度
*/
void OLED_Init_Lora(void)
{
	RCC->APB2ENR|=1<<2;    //使能PORTA时钟 
	RCC->APB2ENR|=1<<3;    //使能PORTB时钟 

  GPIOA->CRH&=0xFFFFFFF0;
  GPIOA->CRH|=0x00000003;//CS  PA8
	GPIOA->ODR|=1<<8;      //PA8 置高电平 上拉
  GPIOB->CRH&=0x0000FFFF;//PB12-15 0011 推挽输出  最大速率50MHZ
  GPIOB->CRH|=0x33330000;//DC、RES、MOSI、SCK
  GPIOB->ODR|=0xF<<12;      //PB12-15 置高电平 上拉	 
	
	OLED_RST=0;
	delay_ms(100);
	OLED_RST=1; 
	OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
	OLED_WR_Byte(0x02,OLED_CMD);//---set low column address
	OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
	OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
	OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
	OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
	OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
	OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
	OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
	OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
	OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
	OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset	Shift Mapping RAM Counter (0x00~0x3F)
	OLED_WR_Byte(0x00,OLED_CMD);//-not offset
	OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
	OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
	OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
	OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
	OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
	OLED_WR_Byte(0x12,OLED_CMD);
	OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
	OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
	OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
	OLED_WR_Byte(0x02,OLED_CMD);//
	OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
	OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
	OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
	OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7) 
	OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel

	OLED_Clear();		
}



